package cfdt.tools.crypto;

import cfca.sadk.util.EncryptUtil;
import cfdt.tools.util.StringUtils;

/**
 * 类说明
 * 
 * <pre>
 * Modify Information:
 * Author       Date        Description
 * ============ =========== ============================
 * YanHao       2019-3-21   Create this file
 * </pre>
 * 
 */
public class SM4Util {

    /**
     * 加密并Base64编码
     *
     * @return
     */
    public static String encrypt(String plainText, String key) throws Exception {
        return encrypt(plainText.getBytes(), key);
    }

    /**
     * 加密并Base64编码
     *
     * @return
     */
    public static String encrypt(byte[] plainText, String key) throws Exception {
        return new String(EncryptUtil.encryptMessageBySM4(plainText, key), StringUtils.DEFAULT_CHARSET);
    }

    /**
     * Base64解码并解密
     *
     * @return
     */
    public static String decrypt(String cipherText, String key) throws Exception {
        return decrypt(cipherText.getBytes(), key);
    }

    /**
     * Base64解码并解密
     *
     * @return
     */
    public static String decrypt(byte[] cipherText, String key) throws Exception {
        return new String(EncryptUtil.decryptMessageBySM4(cipherText, key), StringUtils.DEFAULT_CHARSET);
    }

}
